/// <summary>
/// Copyright 2010 Ivo Limmen (ivo@limmen.org)
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// 
/// http://www.apache.org/licenses/LICENSE-2.0
/// 
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// </summary>

using System;
using System.Xml;
using System.IO;
using Services.Configuration.Api;
using System.Xml.Schema;
using System.Reflection;

namespace Services.Configuration
{
	public class ConfigurationLoader : IConfigurationLoader
	{
		private string directory;
		
		private string appName;

		public ConfigurationLoader(string directory, string appName)
		{
			this.directory = directory;
			this.appName = appName.ToLower().Replace(" ", "") + ".xml";
		}
		
		public XmlDocument Load() 
		{
			string fileName = Path.Combine(this.directory, appName);
			
			XmlDocument document = new XmlDocument();
			document.Schemas.Add(LoadSchema());
			document.Load(fileName);
			
			return document;
		}
		
		private XmlSchema LoadSchema() 
		{
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream resourceStream = assembly.GetManifestResourceStream("Services.Configuration.configuration.xsd");
            StreamReader stream = new StreamReader(resourceStream);
			
			return XmlSchema.Read(stream, new ValidationEventHandler(SchemaReadError));
		}
	
		private void SchemaReadError(object e, ValidationEventArgs arguments) 
		{			
		}
	}
}
